package com.klun.designMode.proxy.myProxy;

import com.klun.designMode.proxy.JDKProxy.Jack;
import com.klun.designMode.proxy.JDKProxy.Person;
import sun.misc.ProxyGenerator;

import java.io.FileOutputStream;

/**
 * 自定义代理类测试
 */
public class ZdyTest {
    public static void main(String[] args) throws Exception {

        Person person = (Person) new ZdyMeiPo().getInstance(new Jack());
        person.findLove();
        System.out.println("person的类 "+person.getClass());

        byte[] data = ProxyGenerator.generateProxyClass("$Proxy0", new Class[]{person.getClass()});
        FileOutputStream os = new FileOutputStream("E:/klun/arithmetic/target/classes/com/klun/designMode/proxy/JDKProxy/$Proxy0.class");
        os.write(data);
        os.close();

//        try {
//            Person person = (Person) new MyProxy().getInstance(new Jack());
//            person.findLove();
//            System.out.println(person.getClass());
//            //原理：
//            //1.拿到被代理对象的引用，然后获取它的接口
//            //2.JDK代理重新生成一个类，同时实现我们给的代理对象所实现的接口
//            //3.把被代理对象的引用也拿到了
//            //4.重新动态生成了一个class字节码
//            //5.然后编译
//            //E:\klun\arithmetic\target\classes\com\klun\designMode\proxy\JDKProxy\Person.class
//            byte[] data = ProxyGenerator.generateProxyClass("$Proxy0", new Class[]{person.getClass()});
//            FileOutputStream os = new FileOutputStream("E:\\klun\\arithmetic\\target\\classes\\com\\klun\\designMode\\proxy\\JDKProxy/$Proxy0.class");
//            os.write(data);
//            os.close();
//        } catch (Exception e) {
//            e.printStackTrace();
//        }

    }
}
